<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary"
                 @click="showAddCate">添加分类</el-button>
      <tree-grid index-text="#"
                 border
                 :expand-type="false"
                 :selection-type="false"
                 show-index
                 :data="cates"
                 :columns="columns">
        <template slot-scope="scope"
                  slot="isOK">
          <i v-if="!scope.row.cat_deleted"
             class="el-icon-success"
             style="color:green"></i>
          <i v-else
             class="el-icon-error"
             style="color:red"></i>
        </template>
        <template slot-scope="scope"
                  slot="order">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1"
                  type="success">二级</el-tag>
          <el-tag v-else
                  type="warning">三级</el-tag>
        </template>
        <template slot="set">
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini">编辑</el-button>
          <el-button type="danger"
                     icon="el-icon-delete"
                     size="mini">删除</el-button>
        </template>
      </tree-grid>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryData.pagenum"
                     :page-sizes="[5,10,20,30]"
                     :page-size="queryData.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
    <el-dialog @close="closeAddCate"
               title="添加分类"
               :visible.sync="addCateDialogVisible"
               width="50%">
      <el-form ref="addCateFormRef"
               :rules="addCateRules"
               :model="addCateForm"
               label-width="100px">
        <el-form-item prop="cat_name"
                      label="分类名称">
          <el-input v-model="addCateForm.cat_name">
          </el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader v-model="cascadervalue"
                       :options="parentCate"
                       :props="cascaderProps"
                       @change="cascaderHandleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取消</el-button>
        <el-button type="primary"
                   @click="submitAddCate">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        pagenum: 1,
        pagesize: 10
      },
      cates: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'set'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '输入分类名称' },
          { min: 2, max: 8, message: '长度2到8' }
        ]
      },
      parentCate: [],
      cascadervalue: [],
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      }
    }
  },

  created () {
    this.getGoodsCate()
  },

  methods: {
    async getGoodsCate () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.cates = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsCate()
    },
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsCate()
    },
    async showAddCate () {
      this.addCateDialogVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.parentCate = res.data
    },
    cascaderHandleChange () {
      if (this.cascadervalue.length > 0) {
        this.addCateForm.cat_level = this.cascadervalue.length
        this.addCateForm.cat_pid = this.cascadervalue[this.cascadervalue.length - 1]
      } else {
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      }
    },
    closeAddCate () {
      this.$refs.addCateFormRef.resetFields()
      this.cascadervalue = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    submitAddCate () {
      this.$refs.addCateFormRef.validate(async flag => {
        if (!flag) return this.message.error('填写必要内容')
        const { data: res } = await this.$axios.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('创建失败')
        this.getGoodsCate()
        this.$message.success('创建成功')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.zk-table {
  margin: 15px 0;
}
.el-cascader {
  width: 100%;
}
</style>
